package panel

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"sola/common"
	"sola/db"
	"sola/utils"
)

func listTags(c *gin.Context) {
	var baseSearchModel common.BaseSearchModel
	if err := c.ShouldBind(&baseSearchModel); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	baseSearchModel.Status = common.SHOW_STATUS_PUBLISHED
	data := make(map[string]interface{})
	count := db.CountTag(&baseSearchModel)
	tags := db.GetTagList(&baseSearchModel)
	data["total"] = count
	data["items"] = tags
	utils.ResponseSuccess(c, data)
	return
}

func listTagArticles(c *gin.Context) {
	var baseSearchModel common.BaseSearchModel
	if err := c.ShouldBindUri(&baseSearchModel); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if baseSearchModel.TagId == "" {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	var tag db.Tag
	tag.TagId = baseSearchModel.TagId
	db.GetTagByTagId(&tag)
	if tag.Id <= 0 {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	data := make(map[string]interface{})
	if tag.TagName == "所有商品" {
		baseSearchModel.TagId = ""
	}
	baseSearchModel.Status = common.SHOW_STATUS_PUBLISHED
	fmt.Println("listTagArticles baseSearchModel:", baseSearchModel)
	count := db.CountArticle(&baseSearchModel)
	articles := db.GetArticleList(&baseSearchModel)
	fmt.Println("listTagArticles articles:", articles)
	data["total"] = count
	data["items"] = articles
	utils.ResponseSuccess(c, data)
	return
}
